package com.example.himalaya.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.himalaya.R;
import com.example.himalaya.base.BaseApplication;
import com.example.himalaya.fragments.RecommendFragment;
import com.example.himalaya.interfaces.OnRetryClickLisener;

public abstract class UILoader extends FrameLayout {

    private OnRetryClickLisener mOnRetryClickListener = null;


    public enum UIStatus {
        LOADING, SUCCESS, NETWORK_ERROR, EMPTY, NONE
    }

    public UIStatus mCurrentStatus = UIStatus.NONE;
    private View LoadingView;
    private View SuccessView;
    private View NetworkErrorView;
    private View EmptyView;


    public UILoader(@NonNull Context context) {
        this(context, null);
    }

    public UILoader(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UILoader(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    //初始化UI
    private void init() {
        switchUIByCurrentStatus();

    }

    public void updateStatus(UIStatus uiStatus) {
        mCurrentStatus = uiStatus;
        //主线程上更新UI
        BaseApplication.getsHandler().post(
                new Runnable() {
                    @Override
                    public void run() {
                        switchUIByCurrentStatus();
                    }
                });
    }

    private void switchUIByCurrentStatus() {

        //1.加载
        if (LoadingView == null) {
            LoadingView = getLoadingView();
            addView(LoadingView);
        }
        //根据状态设置是否可见
        LoadingView.setVisibility(mCurrentStatus == UIStatus.LOADING ? VISIBLE : GONE);

        //2.成功
        if (SuccessView == null) {
            SuccessView = getSuccessView(this);
            addView(SuccessView);
        }
        SuccessView.setVisibility(mCurrentStatus == UIStatus.SUCCESS ? VISIBLE : GONE);

        //3.网络出错
        if (NetworkErrorView == null) {
            NetworkErrorView = getNetworkErrorView();
            addView(NetworkErrorView);
        }
        NetworkErrorView.setVisibility(mCurrentStatus == UIStatus.NETWORK_ERROR ? VISIBLE : GONE);

        //3.数据为空
        if (EmptyView == null) {
            EmptyView = getEmptyView();
            addView(EmptyView);
        }
        EmptyView.setVisibility(mCurrentStatus == UIStatus.EMPTY ? VISIBLE : GONE);

    }

    private View getEmptyView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_empty_view, this, false);
    }

    private View getNetworkErrorView() {
        View networkErrorView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_networkerror_view, this, false);
        networkErrorView.findViewById(R.id.network_error_icon).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //重新获取数据
                if (mOnRetryClickListener != null) {
                    mOnRetryClickListener.onRetryClick();
                }
            }
        });

        return networkErrorView;
    }

    protected abstract View getSuccessView(ViewGroup container);

    private View getLoadingView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_loading_view, this, false);

    }

    public void setOnRetryClickListener(OnRetryClickLisener listener) {
        this.mOnRetryClickListener = listener;
    }


}
